﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class MyProfile : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadProfile();

    }

    private void LoadProfile()
    {
        // Get membership user.
        MembershipUser user = Membership.GetUser(User.Identity.Name);

        lblUserName.Text = user.UserName;
        txtFirstName.Text = Profile.FirstName;
        txtLastName.Text = Profile.LastName;
        txtEmail.Text = user.Email;
        txtCompany.Text = Profile.Company;
        txtTaxId.Text = Profile.TaxId;
        txtAddress.Text = Profile.StreetAddress;
        txtCity.Text = Profile.City;
        try
        {
            lstState.Items.FindByValue(Profile.State).Selected = true;
        }
        catch { }
        
        txtZip.Text = Profile.Zip;
        txtPhoneNumber.Text = Profile.Phone;

        pnlMyProfile.Visible = true;
        pnlConfirmation.Visible = false;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        SaveProfile();
    }

    private void SaveProfile()
    {
        if (Page.IsValid)
        {
            Profile.DisplayName = txtFirstName.Text + " " + txtLastName.Text;
            Profile.FirstName = txtFirstName.Text;
            Profile.LastName = txtLastName.Text;

            MembershipUser user = Membership.GetUser(User.Identity.Name);
            user.Email = txtEmail.Text;
            
            try
            {
                Membership.UpdateUser(user);
            }
            catch
            {
            }

            Profile.Company = txtCompany.Text;
            Profile.TaxId = txtTaxId.Text;
            Profile.StreetAddress = txtAddress.Text;
            Profile.City = txtCity.Text;
            Profile.State = lstState.SelectedValue;
            Profile.Zip = txtZip.Text;
            Profile.Phone = txtPhoneNumber.Text;

            pnlMyProfile.Visible = false;
            pnlConfirmation.Visible = true;
        }

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx", true);
    }
    protected void btnChange_Click(object sender, EventArgs e)
    {
        Response.Redirect("ChangePassword.aspx", true);
    }
}
